// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({env: 'yyf-1gde92y2b5d3dfd2'})

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.action) {
    case 'rootlogin': {  //窗口阿姨登录
      return await root_login()
    }
    case 'orderinfo': { // 所有订单信息
      return await order_info(event)
    }
    case 'orderinfobydesc': { // 所有订单信息时间降序
      return await order_info_by_desc(event)
    }
    case 'orderinfobyasc': { // 所有订单信息时间升序
      return await order_info_by_asc(event)
    }
    case 'order': { // 待取餐订单信息
      return await order(event)
    }
    case 'orderbycondition': { // 根据条件查询订单信息
      return await order_by_condition(event)
    }
    case 'orderupdate': { // 修改订单状态信息
      return await order_update(event)
    }
    case 'ordertime': { // 定时修改订单状态信息
      return await order_time(event)
    }
    case 'mealinfo': { // 所有饭菜信息
      return await meal_info(event)
    }
    case 'addmeal': {  //增加一个菜单的信息
      return await add_meal(event)
    }
    case 'mealbyid': { //根据id去查询菜单
      return await meal_by_id(event)
    }
    case 'editmeal': {  //修改一个菜单的信息
      return await edit_meal(event)
    }
    case 'delmeal': {  //删除菜单
      return await del_meal(event)
    }
    default: {  //其他默认返回空，没有数据
      return
    }
  }
}

async function root_login() {
  return cloud.database().collection("window")
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}

async function order_info(event) {
  return cloud.database().collection("order_info")
  .where({
    "window.id": event.window_id,
    "canteen.id": event.canteen_id
  })
  .orderBy('order_status','asc')
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}

async function order_info_by_desc(event) {
  return cloud.database().collection("order_info")
  .where({
    "window.id": event.window_id,
    "canteen.id": event.canteen_id
  })
  .orderBy('order_time','desc')
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}

async function order_info_by_asc(event) {
  return cloud.database().collection("order_info")
  .where({
    "window.id": event.window_id,
    "canteen.id": event.canteen_id
  })
  .orderBy('order_time','asc')
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}


async function order(event) {
  return cloud.database().collection("order_info")
  .where({
    order_status: "0",
    "window.id": event.window_id,
    "canteen.id": event.canteen_id
  })
  .orderBy('order_time','desc')
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}


async function order_by_condition(event) {
  return cloud.database().collection("order_info")
  .where({
    "student.name": cloud.database().RegExp({
      //以name为关键字进行匹配
      regexp: event.value,
      //输入框输入的内容
      options: 'i',
      //大小写不区分
      }),
    "window.id": event.window_id,
    "canteen.id": event.canteen_id
  })
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}
async function order_update(event) {
  return cloud.database().collection("order_info").doc(event.id)
  .update({
    data: {
      order_status: event.order_status
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}
async function order_time(event) {
  return cloud.database().collection("order_info")
  .where({
    order_status: "0"
  })
  .update({
    data: {
      order_status: "2"
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}
async function meal_info(event) {
  return cloud.database().collection("meal")
  .where({
    "window.window_id": event.window_id,
    "canteen.canteen_id": event.canteen_id
  })
  .orderBy('meal_status','asc')
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}

async function add_meal(event) {
  return cloud.database().collection("meal")
  .add({
    data: {
      meal_name: event.meal_name,
      meal_num: event.meal_num,
      meal_price: event.meal_price,
      meal_detail: event.meal_detail,
      meal_img: event.meal_img,
      meal_img2: event.meal_img2,
      meal_img3: event.meal_img3,
      meal_status: event.meal_status,
      meal_type:event.meal_type,
      meal_week_count: event.meal_week_count,
      meal_totle_count: event.meal_totle_count,
      window: event.window,
      canteen: event.canteen
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
  
}

async function meal_by_id(event) {
  return cloud.database().collection("meal")
  .where({
    _id: event.id
  })
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });;
}

async function edit_meal(event) {
  return cloud.database().collection("meal").doc(event.id)
  .update({
    data: {
      meal_name: event.meal_name,
      meal_num: event.meal_num,
      meal_price: event.meal_price,
      meal_detail: event.meal_detail,
      meal_img: event.meal_img,
      meal_img2: event.meal_img2,
      meal_img3: event.meal_img3,
      meal_status: event.meal_status,
      meal_type:event.meal_type,
      meal_week_count: event.meal_week_count,
      meal_totle_count: event.meal_totle_count,
      window: event.window,
      canteen: event.canteen
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });

}

async function del_meal(event) {
  return cloud.database().collection("meal")
  .doc(event.id)
  .remove()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}